﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Bpms.JsLibrary
{
    /// <summary>
    /// Js库
    /// </summary>
    public enum JsLib
    {
        jquery,
        jquery_cookie,
        jquery_form,
        jquery_ui,
        jquery_alert,
        jquery_blockUI,
        flexigrid,
        effects_blind,
        effects_bounce,
        effects_clip,
        effects_core,
        effects_drop,
        effects_explode,
        effects_fold,
        effects_highlight,
        effects_pulsate,
        effects_scale,
        effects_shake,
        effects_slide,
        effects_transfer,
        ui_accordion,
        ui_core,
        ui_datepicker,
        ui_dialog,
        ui_draggable,
        ui_droppable,
        ui_progressbar,
        ui_resizable,
        ui_selectable,
        ui_slider,
        ui_sortable,
        ui_tabs
    }

    /// <summary>
    /// JS库类
    /// </summary>
    public class Js
    {
        /// <summary>
        /// js库名称
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// js版本号
        /// </summary>
        public string Version
        {
            get;
            set;
        }

        /// <summary>
        /// 按照js库对象次序依次添加到页面中
        /// </summary>
        /// <param name="scripts">js库对象</param>
        public static void AddJs(params Js[] scripts)
        {
            int index = 0;

            foreach (Js script in scripts)
            {
                script.AddAt(index);
                index++;
            }
        }

        /// <summary>
        /// 返回程序集内的JS路径
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetJs(string name)
        {
            Page page = HttpContext.Current.Handler as Page;
            return page.ClientScript.GetWebResourceUrl(typeof(Js), "JsLibrary.Resources." + name + ".js");
        }

        /// <summary>
        /// 按照js库名称依次添加js库到页面
        /// </summary>
        /// <param name="scriptNames">js库名称</param>
        public static void AddJs(params string[] jsNames)
        {
            int index = 0;

            foreach (string jsName in jsNames)
            {
                new Js() { Name = jsName }.AddAt(index);
                index++;
            }
        }

        /// <summary>
        /// 按照指定的枚举名称来加载JS
        /// </summary>
        /// <param name="jsLibs">js枚举</param>
        public static void AddJs(params JsLib[] jsLibs)
        {
            int index = 0;
            foreach (JsLib js in jsLibs)
            {
                new Js() { Name = js.ToString().Replace("_", ".") }.AddAt(index);
                index++;
            }
        }

        /// <summary>
        /// 添加当前js库到head的指定位置
        /// </summary>
        /// <param name="index">位置</param>
        public void AddAt(int index)
        {
            Page page = HttpContext.Current.Handler as Page;

            if (page != null)
            {
                HtmlHead header = page.Header;
                ClientScriptManager cs = page.ClientScript;
                string key = "_bpms_" + this.Name + "_" + this.Version;
                string url = "JsLibrary.Resources." + this.Name + (string.IsNullOrEmpty(this.Version) ? "" : ("-" + this.Version)) + ".js";

                if (!cs.IsClientScriptBlockRegistered(this.GetType(), key))
                {
                    HtmlGenericControl script = new HtmlGenericControl("script");

                    script.Attributes["type"] = "text/javascript";
                    script.Attributes["language"] = "javascript";
                    script.Attributes["src"] = cs.GetWebResourceUrl(this.GetType(), url);
                    if (header != null)
                    {
                        header.Controls.AddAt(index, script);
                        cs.RegisterClientScriptBlock(this.GetType(), key, "");
                    }
                }
            }
        }
    }
}
